package com.gengral.commons.exception;

import com.gengral.commons.constant.Constant;
import com.gengral.commons.msg.BaseResponse;
import com.gengral.commons.utils.ResponseMsgUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全部异常(自定义异常+其他异常)处理类
 *
 * @author
 * @version Created in 23:09 2020/1/19
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandleConfig {
    /**
     * 全局异常捕捉处理
     *
     * @param businessException 自定义异常
     * @return
     */
    @ExceptionHandler(value = BusinessException.class)
    @ResponseBody
    public BaseResponse handle(BusinessException businessException) {
        log.error(String.valueOf(businessException));
        return ResponseMsgUtil.error(Constant.SYS_CODE_WARN, businessException.getMsg());

    }


    /**
     * 全局异常捕捉处理
     *
     * @param e 全局异常
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public BaseResponse defaultExceptionHandler(Exception e) {
        log.error("系统异常!", e);
        return ResponseMsgUtil.error(Constant.SYS_MSG_ERROR);
    }
}
